import 'package:flutter/material.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/widget/common_widget.dart';
import 'package:flutter_axrjc/widget/dialog.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'logic.dart';

class NoticeSettingPage extends StatelessWidget {
  const NoticeSettingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<NoticeSettingLogic>();
    final state = Get.find<NoticeSettingLogic>().state;

    return Scaffold(
      backgroundColor: const Color(0xffF3F6F9),
      appBar: AppBar(
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
        titleSpacing: -10,
        elevation: 0,
        centerTitle: true,
        title: const Text(
          "提醒设置",
          style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
        ),
      ),
      body: SmartRefresher(
        header: reFreshHeader(),
        footer: loadFooter(),
        enablePullUp: true,
        enablePullDown: true,
        onLoading: () {
          logic.getNotice(isRefresh: false);
        },
        onRefresh: () {
          logic.getNotice(isRefresh: true);
        },
        controller: logic.refreshController,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.all(8),
              child: Wrap(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(5),
                    child: GestureDetector(
                      onTap: () {
                        state.remindType.value = "0";
                        logic.getNotice(isRefresh: true);
                      },
                      child: Obx(() {
                        return Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 20, vertical: 10),
                            decoration: BoxDecoration(
                                color: state.remindType.value == "0"
                                    ? primaryColor
                                    : Colors.white,
                                borderRadius: BorderRadius.circular(20)),
                            child: const Text(
                              "灯具检测提醒",
                              style: TextStyle(fontSize: 12),
                            ));
                      }),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(5),
                    child: GestureDetector(
                      onTap: () {
                        state.remindType.value = "1";
                        logic.getNotice(isRefresh: true);
                      },
                      child: Obx(() {
                        return Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 20, vertical: 10),
                            decoration: BoxDecoration(
                                color: state.remindType.value == "1"
                                    ? primaryColor
                                    : Colors.white,
                                borderRadius: BorderRadius.circular(20)),
                            child: const Text(
                              "休息提醒",
                              style: TextStyle(fontSize: 12),
                            ));
                      }),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(5),
                    child: GestureDetector(
                      onTap: () {
                        state.remindType.value = "2";
                        logic.getNotice(isRefresh: true);
                      },
                      child: Obx(() {
                        return Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 20, vertical: 10),
                            decoration: BoxDecoration(
                                color: state.remindType.value == "2"
                                    ? primaryColor
                                    : Colors.white,
                                borderRadius: BorderRadius.circular(20)),
                            child: const Text(
                              "护眼操提醒",
                              style: TextStyle(fontSize: 12),
                            ));
                      }),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Obx(() {
                return ListView.builder(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemCount: state.list.length,
                  itemBuilder: (BuildContext context, int index) {
                    var bean = state.list[index];
                    return GestureDetector(
                      onLongPress: () {
                        showCommonDialog(
                            content: "是否删除提醒？",
                            onConfirm: () {
                              Get.back();
                              logic.deleteNotice("${bean.remindId}");
                            },
                            onCancel: () {
                              Get.back();
                            });
                      },
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(10)),
                          padding: const EdgeInsets.all(8.0),
                          child: Row(
                            children: [
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.symmetric(
                                          vertical: 5),
                                      child: Text(
                                        "${bean.remindLable}",
                                        style: const TextStyle(fontSize: 12),
                                      ),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.symmetric(
                                          vertical: 5),
                                      child: Text(
                                        "${bean.remindTime}",
                                        style: const TextStyle(
                                            fontWeight: FontWeight.bold,
                                            fontSize: 20),
                                      ),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.symmetric(
                                          vertical: 5),
                                      child: Text(
                                        getRepeat("${bean.ifRepeat}"),
                                        style:
                                            const TextStyle(color: Colors.grey),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Obx(() {
                                  return Switch(
                                    value: bean.checked.value,
                                    onChanged: (v) {
                                      logic.setStatus(bean, v);
                                    },
                                    activeColor: orangeColor,
                                    thumbColor:
                                        MaterialStateProperty.resolveWith<
                                            Color>((states) => Colors.white),
                                  );
                                }),
                              )
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                );
              }),
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
          shape: const CircleBorder(),
          child: const Icon(Icons.add),
          onPressed: () async {
            await showAddNoticeDialog();
            logic.getNotice(isRefresh: true);
          }),
    );
  }

  String getRepeat(String s) {
    switch (s) {
      case "7":
        return "每天";
      case "6":
        return "周天";
      case "5":
        return "周六";
      case "4":
        return "周五";
      case "3":
        return "周四";
      case "2":
        return "周三";
      case "1":
        return "周二";
      default:
        return "周一";
    }
    /*
    const NoticeRepeatBean(title: "每天", value: 7),
      const NoticeRepeatBean(title: "周一", value: 0),
      const NoticeRepeatBean(title: "周二", value: 1),
      const NoticeRepeatBean(title: "周三", value: 2),
      const NoticeRepeatBean(title: "周四", value: 3),
      const NoticeRepeatBean(title: "周五", value: 4),
      const NoticeRepeatBean(title: "周六", value: 5),
      const NoticeRepeatBean(title: "周天", value: 6),
     */
  }
}
